package ian.android.lovewords.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeManip {
	
	public static int GetSecIntNow()
	{
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.SECOND);
	}
	
	public static int GetMinIntNow()
	{
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.MINUTE);
	}
	
	public static int GetHourIntNow()
	{
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.HOUR_OF_DAY);
	}
	
	public static int GetDayIntNow()
	{
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.DAY_OF_MONTH);
	}
	
	public static int GetYearIntNow()
	{
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.YEAR);
	}
	
	public static int GetMonthIntNow()
	{
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.MONTH)+1;
	}
	
	public static String GetTimeStringNow()
	{
		Calendar now = Calendar.getInstance();
		String time = now.get(Calendar.YEAR) + 
					"-"+(now.get(Calendar.MONTH)+1) +  
					"-"+ now.get(Calendar.DAY_OF_MONTH) + 
					" "+ now.get(Calendar.HOUR_OF_DAY)+
					":"+ now.get(Calendar.MINUTE) + 
					":"+ now.get(Calendar.SECOND);	
		return time;
	}
	
	public static TimeStruct GetTimeStructNow( )
	{
		TimeStruct ts = new TimeStruct();
		ts.Day = GetDayIntNow();
		ts.Hour = GetHourIntNow();
		ts.Min = GetMinIntNow();
		ts.Month = GetMonthIntNow();
		ts.Sec = GetSecIntNow();
		ts.Year = GetYearIntNow();
		return ts;
	}
	
	public static void UpdateTimeStructtoNow( TimeStruct ts )
	{
		ts.Day = GetDayIntNow();
		ts.Hour = GetHourIntNow();
		ts.Min = GetMinIntNow();
		ts.Month = GetMonthIntNow();
		ts.Sec = GetSecIntNow();
		ts.Year = GetYearIntNow();
	}
	
	public static void ClearTimeStruct( TimeStruct ts)
	{
		ts.Day = 0;
		ts.Hour = 0;
		ts.Min = 0;
		ts.Month = 0;
		ts.Sec = 0;
		ts.Year = 0;
	}
	
	public static boolean LessThanSec( TimeStruct LastTime, TimeStruct Now , int offset)
	{
		if(Now.Year>LastTime.Year || Now.Month > LastTime.Month || Now.Day > LastTime.Day
				|| Now.Hour > LastTime.Hour  || Now.Min > LastTime.Min)
			return true;
		else if( Now.Year==LastTime.Year && Now.Month == LastTime.Month && Now.Day == LastTime.Day
				&& Now.Hour == LastTime.Hour && Now.Min == LastTime.Min && Now.Sec-LastTime.Sec>=offset )
			return true;
		else 
			return false;
	}
	
	public static boolean LessThanMin( TimeStruct LastTime, TimeStruct Now , int offset)
	{
		if(Now.Year>LastTime.Year || Now.Month > LastTime.Month || Now.Day > LastTime.Day
				|| Now.Hour > LastTime.Hour )
			return true;
		else if( Now.Year==LastTime.Year && Now.Month == LastTime.Month && Now.Day == LastTime.Day
				&& Now.Hour == LastTime.Hour && Now.Min - LastTime.Min >= offset )
			return true;
		else 
			return false;
	}
	
	public static boolean LessThanHour( TimeStruct LastTime, TimeStruct Now , int offset)
	{
		if(Now.Year>LastTime.Year || Now.Month > LastTime.Month || Now.Day > LastTime.Day)
			return true;
		else if( Now.Year==LastTime.Year && Now.Month == LastTime.Month && Now.Day == LastTime.Day
				&& Now.Hour - LastTime.Hour >= offset )
			return true;
		else 
			return false;
	}
	
	public static boolean LessThanDay( TimeStruct LastTime, TimeStruct Now , int offset)
	{
		if(Now.Year>LastTime.Year || Now.Month > LastTime.Month)
			return true;
		else if( Now.Year==LastTime.Year && Now.Month == LastTime.Month 
				&& Now.Day - LastTime.Day>= offset )
			return true;
		else 
			return false;
	}
	
	public static int getDaysPassed( TimeStruct StartDate, TimeStruct Now )
	{
		int day=0; 
	    SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
	    //String s1="2006-04-21";
	    //String s2="2006-04-25";
	    Date xxx1=new Date();
	    Date xxx2=new Date();
	    String s1 = "";
	    String s2 = "";
	    s1 = s1 + StartDate.Day + "-" + StartDate.Month + "-" + StartDate.Day;
	    s2 = s2 + Now.Day + "-" + Now.Month + "-" + Now.Day;
	    
	    try {
			xxx1 = sf.parse(s1);
		    xxx2 = sf.parse(s2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    day = (int) ((xxx2.getTime() - xxx1.getTime()) / 3600 / 24 / 1000);
	    day++;
	    return day;
	}
}
